﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace Movies
{
    public partial class FormSelectOrAddMovie : Form
    {
        private MovieDetails currectMovieDetails;
        private String saveLocation;
        private bool refreshOnClose = false;
        public FormSelectOrAddMovie(MovieDetails movieDetails,String folderLocation, bool refOnClose)
        {
            InitializeComponent();
            currectMovieDetails = movieDetails;
            saveLocation = folderLocation;
            refreshOnClose = refOnClose;
        }

        public MovieDetails SelectMovie()
        {
            this.ShowDialog();
            return currectMovieDetails;
        }

        private void cancel_Click(object sender, EventArgs e)
        {
            refreshOnClose = false;
            this.Dispose();
        }


        private void FormSelectOrAddMovie_Load(object sender, EventArgs e)
        {
            if (currectMovieDetails != null)
            {
                title.Text = currectMovieDetails.Name;
                year.Text = currectMovieDetails.Year;
            }

            if (currectMovieDetails.SearchResult != null)
            {
            
            
                foreach (String[] result in currectMovieDetails.SearchResult)
                {
                    int index = movieOptionsList.Rows.Add(result);
                    DataGridViewRow row = movieOptionsList.Rows.SharedRow(index);
                    row.Tag = result[2];
                }
            }

        }

        private void selectButton_Click(object sender, EventArgs e)
        {

            try
            {
                movieOptionsList.Visible = false;
                progressBar.Visible = true;
                progressBar.Value = 0;
                DataGridViewRow row = movieOptionsList.CurrentRow;
                String imdbId = row.Tag.ToString();
                progressBar.Value = 10;
                MovieDetails movie = HelperDefaultMovieSelector.GetMovieByImdbID(imdbId);
                movie.LoadLocation = saveLocation;
                movie.DefSaveWithImage = true;
                progressBar.Value = 15;
                movie.Save();
                currectMovieDetails = movie;
                progressBar.Value = 20;
                this.Dispose();

            }
            catch (Exception ex)
            {
                HelperContentProvider.OmitWarning(ex);
                movieOptionsList.Visible = true;
                progressBar.Visible = false ;
            }
        }

        private void searchButton_Click(object sender, EventArgs e)
        {
            movieOptionsList.Visible = false;
            progressBar.Visible = true;

            movieOptionsList.Rows.Clear();
            progressBar.Value = 10;
            IMDBAPI.ImdbMovieSearchResults movies = HelperDefaultMovieSelector.SearchMovieByName(title.Text.Trim(), year.Text.Trim());
            if (movies != null && movies.Movies != null)
            {
                foreach (IMDBAPI.ImdbSearchItem movieResult in movies.Movies)
                {
                    string[] result = movieResult.SearchResult;
                    int rowIndex= movieOptionsList.Rows.Add(result);
                    DataGridViewRow row = movieOptionsList.Rows.SharedRow(rowIndex);
                    row.Tag = result[2];
                }
            }
            progressBar.Value = 20;
            progressBar.Visible = false;
            movieOptionsList.Visible = true;
        }

        private void movieOptionsList_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            selectButton_Click(null,null);
        }

        private void movieOptionsList_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Return)
            {
                e.Handled = true;
                selectButton_Click(null, null);
            }
        }

        private void FormSelectOrAddMovie_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (refreshOnClose)
            {
                WinDisplayControllor.ApplyChangesOnLoadedWin();
            }
        }
    }
}
